/*
 * Created on 5/Abr/2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.cyborgas.xmltool;

/**
 * @author rino
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.prefs.Preferences;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

public class Util {
	public static Preferences preferences;
	public static Preferences settings;
	
	private static final String PREFERENCES_NODE = "/org/cyborgas/xmltool";
	private static ImageRegistry image_registry;
	private static HashMap xsltOutpoutProfiles = new HashMap();

	static {
		preferences = Preferences.userRoot().node(PREFERENCES_NODE);
		settings = Preferences.systemRoot().node(PREFERENCES_NODE);
		
		XsltOutpoutProfile profile = new XsltOutpoutProfile();
		profile.suffix = ".html";
		profile.writer = XsltOutpoutProfile.WRITER_HTML;
		profile.format = XsltOutpoutProfile.FORMAT_PRETTY_PRINT;
		profile.encoding = null;
		profile.xhtml = false; 
		profile.expandEmptyElements = false;
		xsltOutpoutProfiles.put("html", profile);
		
		profile = new XsltOutpoutProfile();
		profile.suffix = ".xml";
		profile.writer = XsltOutpoutProfile.WRITER_XML;
		profile.format = XsltOutpoutProfile.FORMAT_PRETTY_PRINT;
		profile.encoding = "UTF-8";
		profile.xhtml = true; 
		profile.expandEmptyElements = true;
		xsltOutpoutProfiles.put("xml", profile);
	}
	

	public static URI newURL(String url_name) {
		try {
			return new URI(url_name);
		} catch (URISyntaxException e) {
			throw new RuntimeException("Malformed URL " + url_name, e);
		}
	}

	public static Image getImageResource(String key) {
		return getImageRegistry().get(key);
	}
	public static ImageDescriptor getImageDescriptor(String key) {
		return getImageRegistry().getDescriptor(key);
		//return ImageDescriptor.createFromFile(Util.class, "icons/" + key + ".gif");
	}

	private static ImageRegistry getImageRegistry() {
		if (image_registry == null) {
			image_registry = new ImageRegistry(new org.eclipse.swt.widgets.Display());
			
			// main app icon
			image_registry.put(
					"XmlTool",
					ImageDescriptor.createFromFile(Util.class, "icons/AppIcon-32.gif"));

			// generic icons
			image_registry.put(
					"error",
					ImageDescriptor.createFromFile(Util.class, "icons/error.gif"));
			image_registry.put(
					"warning",
					ImageDescriptor.createFromFile(Util.class, "icons/warning.gif"));
			
			// xml nodes
			image_registry.put(
					"attribute",
					ImageDescriptor.createFromFile(Util.class, "icons/attribute.gif"));
			image_registry.put(
					"element",
					ImageDescriptor.createFromFile(Util.class, "icons/element.gif"));
			image_registry.put(
					"comment",
					ImageDescriptor.createFromFile(Util.class, "icons/comment.gif"));
			image_registry.put(
					"pi",
					ImageDescriptor.createFromFile(Util.class, "icons/pi.gif"));
			
			// actions
			image_registry.put(
					"fileopen",
					ImageDescriptor.createFromFile(Util.class, "icons/fileopen.gif"));
			image_registry.put(
					"filesave",
					ImageDescriptor.createFromFile(Util.class, "icons/filesave.gif"));
			image_registry.put(
					"run",
					ImageDescriptor.createFromFile(Util.class, "icons/run.gif"));
			image_registry.put(
					"find",
					ImageDescriptor.createFromFile(Util.class, "icons/find.gif"));
			image_registry.put(
					"run",
					ImageDescriptor.createFromFile(Util.class, "icons/run.gif"));

			// main windows tabs
			image_registry.put(
					"XPathTab",
					ImageDescriptor.createFromFile(Util.class, "icons/find.gif"));
			image_registry.put(
					"XslTransformTab",
					ImageDescriptor.createFromFile(Util.class, "icons/run.gif"));
			
		}
		return image_registry;
	}

	public static Document getDocument(URI url) throws IOException, JSONException, DocumentException {
		Document result;
		if (url.toString().endsWith(".json") || url.toString().endsWith(".json.txt")) {
			// Load JSON file
			String json = readFile(url);
			JSONObject object = new JSONObject(json);
			String xml = "<?xml version=\"1.0\"?>" + XML.toString(object, "json");
			StringReader reader = new StringReader(xml);
			
			SAXReader saxReader = new SAXReader();
			result = saxReader.read(reader);
		} else {
			SAXReader saxReader = new SAXReader();
			result = saxReader.read(url.toURL());
		}
		return result;
	}

	public static String readFile(File file) throws IOException  {
		return readFile(file.toURI());
	}

	public static String readFile(URI url) throws IOException  {
		StringBuffer sb = new StringBuffer(1024);
		BufferedReader reader = new BufferedReader(new InputStreamReader(url.toURL().openStream()));

		char[] buffer = new char[1024];
		int count;
		do {
			count = reader.read(buffer);
			if (count != -1) {
				sb.append(buffer, 0, count);
			}
		} while (count != -1);
		reader.close();
		
		return sb.toString();
	}

	public static void saveFile(File file, String text) throws IOException {
		FileWriter writer = new FileWriter(file);
		writer.write(text);
		writer.close();
	}

	public static XsltOutpoutProfile getXsltOutpoutProfile(String profileName) {
		XsltOutpoutProfile profile = (XsltOutpoutProfile) xsltOutpoutProfiles.get(profileName);
		if (profile == null) {
			profile = (XsltOutpoutProfile) xsltOutpoutProfiles.get("html");
		}
		return profile;
	}




}

